<template>
  <div class="update-gender">
    <van-picker
      title="更新性别"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @cancel="$emit('colse')"
      @confirm="onConfirm"
      @change="onPickerChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localGender: 0
    }
  },
  watch: {},
  computed: {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '正在保存中.......',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })
      try {
        const localGender = this.localGender

        await updateUserProfile({
          gender: localGender
        })
        //   console.log(data);
        // 更新视图
        this.$emit('input', localGender)
        // 关闭弹层
        this.$emit('close')
        // 提示内容
        this.$toast.success('更新性别成功')
      } catch (err) {
        this.$toast('修改性别失败')
      }
    },
    onPickerChange (picker, value, index) {
      // console.log(picker,value,index);
      this.localGender = index
    }
  },
  created () {},
  mounted () {}
}
</script>
<style lang="less" scoped></style>
